<template>
  <!-- 弹出新增窗口 
        title 窗口的标题
        :visible.sync 当它true的时候，窗口会被弹出
        -->
  <el-dialog
    :title="pojo.id == null ? '新增会员' : '修改会员'"
    :visible.sync="dialogVisible"
    :before-close="handleClose"
    :close-on-click-modal="false"
    width="500px"
  >
    <el-form
      :rules="rules"
      ref="pojoForm"
      label-width="100px"
      label-position="right"
      style="width: 400px"
      :model="pojo"
    >
      <el-form-item label="会员卡号" prop="cardNum">
        <el-input v-model="pojo.cardNum"></el-input>
      </el-form-item>
      <el-form-item label="会员姓名" prop="name">
        <el-input v-model="pojo.name"></el-input>
      </el-form-item>

      <el-form-item label="会员生日" prop="birthday">
        <!-- value-format 是指定绑定值的格式 -->
        <el-date-picker
          style="width: 200px"
          value-format="timestamp"
          v-model="pojo.birthday"
          type="date"
          placeholder="会员生日"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="pojo.phone"></el-input>
      </el-form-item>
      <el-form-item label="开卡金额" prop="money">
        <el-input v-model="pojo.money"></el-input>
      </el-form-item>
      <el-form-item label="可用积分" prop="integral">
        <el-input v-model="pojo.integral"></el-input>
      </el-form-item>
      <el-form-item label="支付类型" prop="payType">
        <el-select
          v-model="pojo.payType"
          placeholder="支付类型"
          style="width: 110px"
        >
          <!-- 不要忘记 payTypeOptions 绑定到data中 -->
          <el-option
            v-for="option in payTypeOptions"
            :key="option.type"
            :label="option.name"
            :value="option.type"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="会员地址" prop="address">
        <el-input type="textarea" v-model="pojo.address"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="submitForm('pojoForm')"
        >确 定</el-button
      >
    </div>
  </el-dialog>
</template>

<script>
import memberApi from "@/api/member";
export default {
  data() {
    return {
      rules: {
        // 校验规则
        cardNum: [{ required: true, message: "卡号不能为空", trigger: "blur" }],
        name: [{ required: true, message: "姓名不能为空", trigger: "blur" }],
        payType: [
          { required: true, message: "支付类型不能为空", trigger: "change" },
        ],
        birthday: [
          { required: true, message: "生日不能为空", trigger: "blur" },
        ],
        phone: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        address: [{ required: true, message: "地址不能为空", trigger: "blur" }],
      },
    };
  },

  props: ["dialogVisible", "pojo", "closeDialog", "payTypeOptions"],

  components: {},

  methods: {
    // 提交新增数据
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //提交表单
          this.addOrUpdate();
        }
      });
    },

    async addOrUpdate() {
      await memberApi.add(this.pojo);
      this.$message("成功");
      //新增成功，刷新列表数据
      this.handleClose(); // 关闭窗口
    },

    handleClose() {
      console.log("关闭了添加或修改窗口");

      this.$nextTick(() => {
        this.$refs["pojoForm"].resetFields();
        this.closeDialog();
      });
    },
  },
};
</script>

<style scoped>
</style>